package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyBeiJinJingDing;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Map;

/**
 * 安心分期-北京金鼎三选80
 * @author 李源聪
 */
@ApiPlan(productId = "211", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5BeiJinJingDing implements ApiOrgPhoneMd5ConvertHandler {
    private static final String KEY = "cY7N4pQaW1zXVrM2";

    private static final String CHANNEL_CODE = "707";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        String mobile = apiDataExchangeParameter.getPhoneMd5();
        long currentTimeMillis = System.currentTimeMillis();
        String signStr = CHANNEL_CODE + mobile + currentTimeMillis + KEY;

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", mobile);
            newMap.put("channel_code", CHANNEL_CODE);
            newMap.put("timestamp", currentTimeMillis);
            newMap.put("sign", DigestUtils.md5Hex(signStr).toUpperCase());
        });

        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyBeiJinJingDing orgApply = new OrgApplyBeiJinJingDing();

        //  赋值进件属性
        orgApply.setChannel_code("anxin");
        orgApply.setUserName(apiApplyParameter.getUserName());
        // 手机号
        orgApply.setPhone(apiApplyParameter.getPhone());
        //  获取年龄
        orgApply.setAge(apiApplyParameter.getAge());
        // 城市
        orgApply.setCity(apiApplyParameter.getCity());
        /* 贷款金额 */
        orgApply.setLoanAmount(apiApplyParameter.getLoanAmount());
        //  性别-男/女
        orgApply.setSex(apiApplyParameter.getSex());
        // 房 有/无
        orgApply.setRealEstateInfo(apiApplyParameter.getRealEstateInfo());
        // 车 有/无
        orgApply.setCarInfo(apiApplyParameter.getCarInfo());
        // 社保有/无
        orgApply.setSocialSecurity(apiApplyParameter.getSocialSecurity());
        // 公积金 有/无
        orgApply.setProvidentFund(apiApplyParameter.getProvidentFund());
        //  工作 有/无
        orgApply.setCareer(apiApplyParameter.getCareer());
        // 保险保单 有/无
        orgApply.setInsurancePolicy(apiApplyParameter.getInsurancePolicy());
        // 信用卡 有/无
        orgApply.setCreditLimit(apiApplyParameter.getCreditLimit());
        // 银行代发-有/无
        orgApply.setSalaryPayMethod(apiApplyParameter.getSalaryPayMethod());
        // 芝麻分 650，700
        orgApply.setSesameCredit(apiApplyParameter.getSesameCredit());
        String signStr = apiApplyParameter.getPhone() + KEY;
        orgApply.setSign(DigestUtils.md5Hex(signStr).toUpperCase());
        orgApply.setChannel_code(CHANNEL_CODE);
        return JsonUtils.toJson(orgApply);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 2000)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
